package ui
{
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	
	import Common.MapDirection;
	import map.MapGrid;
	
	import net.TeamNetHandler;

	public class TopUIHandler
	{
		public var topLeftUI:MovieClip = null;
		public var topRightUI:MovieClip = null;
		
		
		public function TopUIHandler()
		{
			
		}
		
		
		public static var instance:TopUIHandler = null;
		public static function Instance():TopUIHandler
		{
			if ( instance == null )
			{
				instance = new TopUIHandler();
			}
			
			return instance;
		}
		
		
		public function init( left:MovieClip , right:MovieClip ):void
		{
			topLeftUI = left;
			topRightUI = right;
			
			topLeftUI.teamButton.addEventListener( MouseEvent.CLICK , onClickTeamButton );
			
			var date:Date = new Date();
			TopUIHandler.instance.setTime( date.hours , date.minutes );
			date = null;
		}
		
		
		public function setTime( h:int , m:int ):void
		{
			if ( topRightUI )
				topRightUI.SetTime( h , m );
		}
		
		
		public function onClickTeamButton( e:MouseEvent ):void
		{
			var mainPlayer:GamePlayer = GameManager.instance.MainPlayer;
			
			if ( mainPlayer.team )
			{
				TeamNetHandler.sendLevelTeam( mainPlayer.ID );
				return;
			}
			
			var direction:int = mainPlayer.Direction();
			var pos:MapGrid = mainPlayer.MMapGrid;
			var pos1:MapGrid = pos.clone();
			pos1.move( direction );
			
			
			var playerList:Object = GameCreatureManager.instance.getPlayerScene( pos1.x , pos1.y );
			
			if ( playerList && playerList.num )
			{
				for each ( var player:GamePlayer in playerList )
				{
					TeamNetHandler.sendJoinTeam( player.ID );
					
					return;
				}
			}
			
			
		}
		
		
	}
}


